package led.control.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import led.control.home.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SlidingDrawer;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.ViewFlipper;

public class BluetoothConn extends Activity implements OnGestureListener{
	// Debugging
	private static final String TAG = "BluetoothConn";
	private static final boolean D = true;

	private static String versionNo = "1.0.0";

	// Key names received from the BluetoothChatService Handler
	public static final String DEVICE_NAME = "device_name";
	public static final String TOAST = "toast";

	public static final String NOTIFY_UI = "NOTIFY_UI";
	public static final String INCOMING_MSG = "INCOMING_MSG";
	public static final String OUTGOING_MSG = "OUTGOING_MSG";
	public static final String ALERT_MSG = "ALERT_MSG";
	public static final String KEY_ECHO_PREF = "KEY_ECHO_PREF";
	public static final String DEVICE_ADDRESS = "device_address";
	public static final String DISCONNECT_DEVICE_ADDRESS = "disconnected_device_address";

	// Intent request codes
	private static final int REQUEST_CONNECT_DEVICE = 1;
	private static final int REQUEST_ENABLE_BT = 2;
	private static final int REQUEST_PREF_SETTING = 3;
	private static final int REQUEST_DISCONNECT_DEVICE = 4;
	private static final int REQUEST_ABOUT_INFO = 5;
	private static final int DIALOG_ABOUT_INFO = 6;
	private static final int FILE_DIALOG_REQUEST = 7;
	
	public static final String TEST_ACTION = "TEST_ACTION";
	
	//Handler meg
	private static final int UPDATETEXTVIEWANDIMAGE = 1;
	private static final int WAITONESECOND = 2;
	
	//Max Value
	private static final int MAXVALUE = 254;

	private TextView pp1,pp2,pp3,pp4,pp5,pp6,pp7,pp8,mCurrentPP;
	private ImageView mImageView1,mImageView2,mImageView3,mImageView4,mImageView5,mImageView6,mImageView7,mImageView8,mCurrentImageView;
	private CircularSeekBar seekbar1,seekbar2,seekbar3,seekbar4,seekbar5,seekbar6,seekbar7,seekbar8;
	private MSeekBarChangListener listener;

//	private TextView mTitle;
	private BluetoothAdapter mBluetoothAdapter = null;
	private NotificationReceiver mNotiReceiver;
	private boolean mMonitor = false;


	private MyButton mHomeBtn0,mHomeBtn1,mHomeBtn2,mHomeBtn3,mHomeBtn4,mHomeBtn5,mHomeBtn6,mHomeBtn7,mHomeBtn8;
	private Button mAtHomeBtn,mOutBtn,mSunnyBtn,mCloudyBtn,mReadingBtn,mGameBtn,mSleepBtn,mMovieBtn,mTrunAllOnBtn,mTrunAllOffBtn;
	private Button mMaxBtn1,mMaxBtn2,mMaxBtn3,mMaxBtn4,mMaxBtn5,mMaxBtn6,mMaxBtn7,mMaxBtn8,
				   mMinBtn1,mMinBtn2,mMinBtn3,mMinBtn4,mMinBtn5,mMinBtn6,mMinBtn7,mMinBtn8,mCurrentMaxButton,mCurrentMinButton;
	
	
	private List<MyButton> mButtonList;
	private MOnClickListener mClickListener;
	private MControlButtonListener mControlBtnListener;
	private MMaxButtonListener mMaxButtonListener;
	private MMinButtonListener mMinButtonListener;

	private GestureDetector detector;
	private ViewFlipper flipper;
	int i = 0;

	//	msg_inttent = new Intent(BluetoothConnController.SEND_MSG_FROM_BT_ACTION);
	//    i.putExtra("MESSAGE", "12345");
	//    i.putExtra("MODE", BluetoothConnController.MSG_MODE_SEND_STRING);
	//    sendBroadcast(i);



	private SharedPreferences mSharedPreferencesForSeekBar,mSharedPreferencesForSwitch;
	private RadioGroup mRadioGroup1,mRadioGroup2,mRadioGroup3,mRadioGroup4,mRadioGroup5,mRadioGroup6,mRadioGroup7,mRadioGroup8,mCurrentRadioGroup;

	private RadioButton mCeiling1,mCeiling2,mCeiling3,mCeiling4,mCeiling5,mCeiling6,mCeiling7,mCeiling8;
	private RadioButton cabinet_light1,cabinet_light2,cabinet_light3,cabinet_light4,cabinet_light5,cabinet_light6,cabinet_light7,cabinet_light8;
	private RadioButton wall_lamp1,wall_lamp2,wall_lamp3,wall_lamp4,wall_lamp5,wall_lamp6,wall_lamp7,wall_lamp8;
	private RadioButton mirror_light1,mirror_light2,mirror_light3,mirror_light4,mirror_light5,mirror_light6,mirror_light7,mirror_light8;
	private RadioButton bedside_lamp1,bedside_lamp2,bedside_lamp3,bedside_lamp4,bedside_lamp5,bedside_lamp6,bedside_lamp7,bedside_lamp8;
	private RadioButton table_lamp1,table_lamp2,table_lamp3,table_lamp4,table_lamp5,table_lamp6,table_lamp7,table_lamp8;
	private RadioButton sofa_lamp1,sofa_lamp2,sofa_lamp3,sofa_lamp4,sofa_lamp5,sofa_lamp6,sofa_lamp7,sofa_lamp8;
	private RadioButton mCurrentRadioButton;

	private ToggleButton mSwitch1,mSwitch2,mSwitch3,mSwitch4,mSwitch5,mSwitch6,mSwitch7,mSwitch8;
	private ToggleButton mCurrentSwitch;

	
	private String key_ceiling1 = "ceiling1";
	private String[] mKeys = new String[]{"ceiling1","ceiling2","ceiling3","ceiling4","ceiling5","ceiling6","ceiling7","ceiling8",
			"cabinet_light3","cabinet_light4","cabinet_light6","cabinet_light7",
			"wall_lamp1","wall_lamp2","wall_lamp3","wall_lamp5","wall_lamp6","wall_lamp7",
			"mirror_light1","mirror_light2","mirror_light3","mirror_light5","mirror_light6","mirror_light7",
			"bedside_lamp3","bedside_lamp6","bedside_lamp7","table_lamp1","sofa_lamp1"};
	
	private List<RadioButton> mRadioButtons;
	private List<RadioGroup> mRadioGroups;
	private List<Button> mMaxButtons;
	private List<Button> mMinButtons;
	private Map<String,Integer> mAllPreferenceForSeekBar;
	private Map<String,Boolean> mAllPreferenceForSwitch;
	private Map<MyButton,CircularSeekBar> MyButtonForBarMap;
	private Map<RadioButton, String> mRadioButtonForKey;
	private Map<MyButton,RadioGroup> mButtonForRadioGroup;
	private Map<MyButton,ToggleButton> mButtonForSwitch;
	private Map<MyButton,TextView> mButtonForPP;
	private Map<MyButton,ImageView> mButtonForImage;
	private Map<RadioButton,String> mRadioButtonForAddr;
	private Map<MyButton,Button> mMyButtonforMaxButton;
	private Map<MyButton,Button> mMyButtonforMinButton;
	private CircularSeekBar mCurrentSeekBar;
	private List<CircularSeekBar> mAllSeekBar;
	private List<ToggleButton> mSwitchs;
	private List<TextView> mPPList;
	private List<ImageView> mImgs;

	private ToneLayer mToneLayer;
	private Bitmap mBitmap;
	private MOnToggleButtleCheckListener mOnToggleButtleCheckListener;
	private MOnRadioButtonCheckListener mOnRadioButtonCheckListener;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		System.out.println("-------------oncreate-----------------");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  // 隐藏状态栏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.main_activity);

		mSharedPreferencesForSeekBar = getSharedPreferences("seekbar_value", MODE_PRIVATE);
		mSharedPreferencesForSwitch = getSharedPreferences("switch_value", MODE_PRIVATE);

		mToneLayer = new ToneLayer(this);

		intAllHomeButton();
		initSeekBar();
		initControlButton();
		detector = new GestureDetector(this);
		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);
		IntentFilter mFilter01, mFilter02;
		mFilter01 = new IntentFilter(INCOMING_MSG);
		mFilter02 = new IntentFilter(OUTGOING_MSG);
		mNotiReceiver = new NotificationReceiver();
		registerReceiver(mNotiReceiver, mFilter01);
		registerReceiver(mNotiReceiver, mFilter02);
		registerReceiver(mNotiReceiver, new IntentFilter(ALERT_MSG));
		registerReceiver(mNotiReceiver, new IntentFilter(BluetoothConnController.GET_SERIVICE_STATUS_EVENT));
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		if (mBluetoothAdapter == null) {
			Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
			finish();
			return;
		}
		SetOtherButtonUncheck(mHomeBtn0);
	}



//	private boolean isInit = false;
	
	private void setSwitchChecked(){
		System.out.println("set switch------------");
//		isInit = true;
		boolean b = mAllPreferenceForSwitch.get(getCurrentKey());
		setMaxMinBtnClickAble(b);
		mCurrentSwitch.setChecked(b);
	}

	private void setMaxMinBtnClickAble(boolean b){
		if(b){
			mCurrentMaxButton.setClickable(true);
			mCurrentMinButton.setClickable(true);
		}else{
			mCurrentMaxButton.setClickable(false);
			mCurrentMinButton.setClickable(false);
		}
	}
	
	
	private void setSeekBarWithValue(int value){
		mCurrentSeekBar.setEnabled(true);
		mCurrentSeekBar.setUserProgress(value);
		mCurrentPP.setTextSize(getResources().getDimension(R.dimen.pecent_fontsize));
		mCurrentPP.setTextColor(getResources().getColor(R.color.yellow));
	}
	
	
	
	private void setSeekBarValue(boolean isInvisible){
		if(isInvisible){
			System.out.println("INVISIBLE ----------------------------");
			mCurrentSeekBar.setEnabled(false);
			mCurrentSeekBar.setUserProgress(mAllPreferenceForSeekBar.get(getCurrentKey()));
//			mCurrentSeekBar.setUserProgress(0);
			mToneLayer.setHue(2);
			mCurrentImageView.setImageBitmap(mToneLayer.handleImage(mBitmap, 2));
			mCurrentPP.setTextSize(getResources().getDimension(R.dimen.pecent_textsize_invisible));
			mCurrentPP.setTextColor(getResources().getColor(R.color.gray));
		}else{
			System.out.println("VISIBLE ----------------------------");
			mCurrentSeekBar.setEnabled(true);
			mCurrentSeekBar.setUserProgress(mAllPreferenceForSeekBar.get(getCurrentKey()));
			mCurrentPP.setTextSize(getResources().getDimension(R.dimen.pecent_fontsize));
			mCurrentPP.setTextColor(getResources().getColor(R.color.yellow));
		}
	}

	private String getCurrentKey(){
		return mRadioButtonForKey.get(mCurrentRadioButton);
	}

	private void FirstReadPerference(){
		for(int i = 0;i < mKeys.length;i++){
			System.out.println(mKeys[i]);
			mAllPreferenceForSeekBar.put(mKeys[i],mSharedPreferencesForSeekBar.getInt(mKeys[i], MAXVALUE/2));
			mAllPreferenceForSwitch.put(mKeys[i], mSharedPreferencesForSwitch.getBoolean(mKeys[i], true));
		}
	}

	private void writePerference(){
		Editor esb = mSharedPreferencesForSeekBar.edit();
		Editor esw = mSharedPreferencesForSwitch.edit();
		for(int i = 0;i < mKeys.length;i++){
			esb.putInt(mKeys[i], mAllPreferenceForSeekBar.get(mKeys[i]));
			esw.putBoolean(mKeys[i], mAllPreferenceForSwitch.get(mKeys[i]));
		}
		esb.commit();
		esw.commit();
	}
	
	
	private void initControlButton(){
		
		mControlBtnListener = new MControlButtonListener();
		
		mAtHomeBtn = (Button) findViewById(R.id.b9);
		mOutBtn = (Button) findViewById(R.id.b10);
		mSunnyBtn = (Button) findViewById(R.id.b11);
		mCloudyBtn = (Button) findViewById(R.id.b12);
		mMovieBtn = (Button) findViewById(R.id.b13);
		mReadingBtn = (Button) findViewById(R.id.b14);
		mGameBtn = (Button) findViewById(R.id.b15);
		mSleepBtn = (Button) findViewById(R.id.b16);
		mTrunAllOnBtn = (Button) findViewById(R.id.b17);
		mTrunAllOffBtn = (Button) findViewById(R.id.b18);
		
		mAtHomeBtn.setOnClickListener(mControlBtnListener);
		mOutBtn.setOnClickListener(mControlBtnListener);
		mSunnyBtn.setOnClickListener(mControlBtnListener);
		mCloudyBtn.setOnClickListener(mControlBtnListener);
		mMovieBtn.setOnClickListener(mControlBtnListener);
		mReadingBtn.setOnClickListener(mControlBtnListener);
		mGameBtn.setOnClickListener(mControlBtnListener);
		mSleepBtn.setOnClickListener(mControlBtnListener);
		mTrunAllOnBtn.setOnClickListener(mControlBtnListener);
		mTrunAllOffBtn.setOnClickListener(mControlBtnListener);
	}
	
	
	
	private void intAllHomeButton(){

		mAllPreferenceForSeekBar = new HashMap<String, Integer>();
		mAllPreferenceForSwitch = new HashMap<String, Boolean>();
		MyButtonForBarMap = new HashMap<MyButton, CircularSeekBar>();
		mRadioButtonForKey = new HashMap<RadioButton, String>();
		mButtonList = new ArrayList<MyButton>();
		mClickListener = new MOnClickListener();
		mRadioButtons = new ArrayList<RadioButton>();
		mRadioGroups = new ArrayList<RadioGroup>();
		mButtonForRadioGroup = new HashMap<MyButton, RadioGroup>();
		mRadioButtonForAddr = new HashMap<RadioButton, String>();
		mSwitchs = new ArrayList<ToggleButton>();
		mButtonForSwitch = new HashMap<MyButton, ToggleButton>();
		mOnToggleButtleCheckListener = new MOnToggleButtleCheckListener();
		mOnRadioButtonCheckListener = new MOnRadioButtonCheckListener();
		mMaxButtonListener = new MMaxButtonListener();
		mMinButtonListener = new MMinButtonListener();
		mMaxButtons = new ArrayList<Button>();
		mMinButtons = new ArrayList<Button>();
		mMyButtonforMaxButton = new HashMap<MyButton, Button>();
		mMyButtonforMinButton = new HashMap<MyButton, Button>();

		mHomeBtn0 = (MyButton) findViewById(R.id.b0);
		mHomeBtn1 = (MyButton) findViewById(R.id.b1);
		mHomeBtn2 = (MyButton) findViewById(R.id.b2);
		mHomeBtn3 = (MyButton) findViewById(R.id.b3);
		mHomeBtn4 = (MyButton) findViewById(R.id.b4);
		mHomeBtn5 = (MyButton) findViewById(R.id.b5);
		mHomeBtn6 = (MyButton) findViewById(R.id.b6);
		mHomeBtn7 = (MyButton) findViewById(R.id.b7);
		mHomeBtn8 = (MyButton) findViewById(R.id.b8);

		mHomeBtn0.setmLayout((LinearLayout)findViewById(R.id.layout_home0));
		mHomeBtn1.setmLayout((LinearLayout)findViewById(R.id.layout_home1));
		mHomeBtn2.setmLayout((LinearLayout)findViewById(R.id.layout_home2));
		mHomeBtn3.setmLayout((LinearLayout)findViewById(R.id.layout_home3));
		mHomeBtn4.setmLayout((LinearLayout)findViewById(R.id.layout_home4));
		mHomeBtn5.setmLayout((LinearLayout)findViewById(R.id.layout_home5));
		mHomeBtn6.setmLayout((LinearLayout)findViewById(R.id.layout_home6));
		mHomeBtn7.setmLayout((LinearLayout)findViewById(R.id.layout_home7));
		mHomeBtn8.setmLayout((LinearLayout)findViewById(R.id.layout_home8));

		mHomeBtn0.setOnClickListener(mClickListener);		
		mHomeBtn1.setOnClickListener(mClickListener);		
		mHomeBtn2.setOnClickListener(mClickListener);
		mHomeBtn3.setOnClickListener(mClickListener);
		mHomeBtn4.setOnClickListener(mClickListener);
		mHomeBtn5.setOnClickListener(mClickListener);
		mHomeBtn6.setOnClickListener(mClickListener);
		mHomeBtn7.setOnClickListener(mClickListener);
		mHomeBtn8.setOnClickListener(mClickListener);

//		mButtonList.add(mHomeBtn0);
		mButtonList.add(mHomeBtn1);
		mButtonList.add(mHomeBtn2);
		mButtonList.add(mHomeBtn3);
		mButtonList.add(mHomeBtn4);
		mButtonList.add(mHomeBtn5);
		mButtonList.add(mHomeBtn6);
		mButtonList.add(mHomeBtn7);
		mButtonList.add(mHomeBtn8);

		//---------------------------------------------------------------------------------
		mSwitch1 = (ToggleButton) findViewById(R.id.switch1);
		mSwitch2 = (ToggleButton) findViewById(R.id.switch2);
		mSwitch3 = (ToggleButton) findViewById(R.id.switch3);
		mSwitch4 = (ToggleButton) findViewById(R.id.switch4);
		mSwitch5 = (ToggleButton) findViewById(R.id.switch5);
		mSwitch6 = (ToggleButton) findViewById(R.id.switch6);
		mSwitch7 = (ToggleButton) findViewById(R.id.switch7);
		mSwitch8 = (ToggleButton) findViewById(R.id.switch8);

		mSwitchs.add(mSwitch1);
		mSwitchs.add(mSwitch2);
		mSwitchs.add(mSwitch3);
		mSwitchs.add(mSwitch4);
		mSwitchs.add(mSwitch5);
		mSwitchs.add(mSwitch6);
		mSwitchs.add(mSwitch7);
		mSwitchs.add(mSwitch8);


		for(int i=0;i < mButtonList.size();i++){
			mButtonForSwitch.put(mButtonList.get(i), mSwitchs.get(i));
			mSwitchs.get(i).setOnCheckedChangeListener(mOnToggleButtleCheckListener);
		}



		//---------------------------------------------------------------------------------

		mCeiling1 = (RadioButton) findViewById(R.id.ceiling1);
		mCeiling2 = (RadioButton) findViewById(R.id.ceiling2);
		mCeiling3 = (RadioButton) findViewById(R.id.ceiling3);
		mCeiling4 = (RadioButton) findViewById(R.id.ceiling4);
		mCeiling5 = (RadioButton) findViewById(R.id.ceiling5);
		mCeiling6 = (RadioButton) findViewById(R.id.ceiling6);
		mCeiling7 = (RadioButton) findViewById(R.id.ceiling7);
		mCeiling8 = (RadioButton) findViewById(R.id.ceiling8);

		cabinet_light3 = (RadioButton) findViewById(R.id.cabinet_light3);
		cabinet_light4 = (RadioButton) findViewById(R.id.cabinet_light4);
		cabinet_light6 = (RadioButton) findViewById(R.id.cabinet_light6);
		cabinet_light7 = (RadioButton) findViewById(R.id.cabinet_light7);

		wall_lamp1 = (RadioButton) findViewById(R.id.wall_lamp1);
		wall_lamp2 = (RadioButton) findViewById(R.id.wall_lamp2);
		wall_lamp3 = (RadioButton) findViewById(R.id.wall_lamp3);
		wall_lamp5 = (RadioButton) findViewById(R.id.wall_lamp5);
		wall_lamp6 = (RadioButton) findViewById(R.id.wall_lamp6);
		wall_lamp7 = (RadioButton) findViewById(R.id.wall_lamp7);

		mirror_light1 = (RadioButton) findViewById(R.id.mirror_light1);
		mirror_light2 = (RadioButton) findViewById(R.id.mirror_light2);
		mirror_light3 = (RadioButton) findViewById(R.id.mirror_light3);
		mirror_light5 = (RadioButton) findViewById(R.id.mirror_light5);
		mirror_light6 = (RadioButton) findViewById(R.id.mirror_light6);
		mirror_light7 = (RadioButton) findViewById(R.id.mirror_light7);

		bedside_lamp3 = (RadioButton) findViewById(R.id.bedside_lamp3);
		bedside_lamp6 = (RadioButton) findViewById(R.id.bedside_lamp6);
		bedside_lamp7 = (RadioButton) findViewById(R.id.bedside_lamp7);

		table_lamp1 = (RadioButton) findViewById(R.id.table_lamp1);

		sofa_lamp1 = (RadioButton) findViewById(R.id.sofa_lamp1);



		mRadioButtons.add(mCeiling1);
		mRadioButtons.add(wall_lamp1);
		mRadioButtons.add(mirror_light1);
		mRadioButtons.add(table_lamp1);
		mRadioButtons.add(sofa_lamp1);

		mRadioButtons.add(mCeiling2);
		mRadioButtons.add(wall_lamp2);
		mRadioButtons.add(mirror_light2);

		mRadioButtons.add(mCeiling3);
		mRadioButtons.add(cabinet_light3);
		mRadioButtons.add(wall_lamp3);
		mRadioButtons.add(mirror_light3);
		mRadioButtons.add(bedside_lamp3);

		mRadioButtons.add(mCeiling4);
		mRadioButtons.add(cabinet_light4);

		mRadioButtons.add(mCeiling5);
		mRadioButtons.add(wall_lamp5);
		mRadioButtons.add(mirror_light5);

		mRadioButtons.add(mCeiling6);
		mRadioButtons.add(cabinet_light6);
		mRadioButtons.add(wall_lamp6);
		mRadioButtons.add(mirror_light6);
		mRadioButtons.add(bedside_lamp6);

		mRadioButtons.add(mCeiling7);
		mRadioButtons.add(cabinet_light7);
		mRadioButtons.add(wall_lamp7);
		mRadioButtons.add(mirror_light7);
		mRadioButtons.add(bedside_lamp7);

		mRadioButtons.add(mCeiling8);


		mRadioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
		mRadioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
		mRadioGroup3 = (RadioGroup) findViewById(R.id.radioGroup3);
		mRadioGroup4 = (RadioGroup) findViewById(R.id.radioGroup4);
		mRadioGroup5 = (RadioGroup) findViewById(R.id.radioGroup5);
		mRadioGroup6 = (RadioGroup) findViewById(R.id.radioGroup6);
		mRadioGroup7 = (RadioGroup) findViewById(R.id.radioGroup7);
		mRadioGroup8 = (RadioGroup) findViewById(R.id.radioGroup8);


		mRadioGroups.add(mRadioGroup1);
		mRadioGroups.add(mRadioGroup2);
		mRadioGroups.add(mRadioGroup3);
		mRadioGroups.add(mRadioGroup4);
		mRadioGroups.add(mRadioGroup5);
		mRadioGroups.add(mRadioGroup6);
		mRadioGroups.add(mRadioGroup7);
		mRadioGroups.add(mRadioGroup8);


//--------------------------------------------------------
		
		
		mMaxBtn1 = (Button) findViewById(R.id.max_btn1);
		mMaxBtn2 = (Button) findViewById(R.id.max_btn2);
		mMaxBtn3 = (Button) findViewById(R.id.max_btn3);
		mMaxBtn4 = (Button) findViewById(R.id.max_btn4);
		mMaxBtn5 = (Button) findViewById(R.id.max_btn5);
		mMaxBtn6 = (Button) findViewById(R.id.max_btn6);
		mMaxBtn7 = (Button) findViewById(R.id.max_btn7);
		mMaxBtn8 = (Button) findViewById(R.id.max_btn8);
//		
//		
		mMinBtn1 = (Button) findViewById(R.id.min_btn1);
		mMinBtn2 = (Button) findViewById(R.id.min_btn2);
		mMinBtn3 = (Button) findViewById(R.id.min_btn3);
		mMinBtn4 = (Button) findViewById(R.id.min_btn4);
		mMinBtn5 = (Button) findViewById(R.id.min_btn5);
		mMinBtn6 = (Button) findViewById(R.id.min_btn6);
		mMinBtn7 = (Button) findViewById(R.id.min_btn7);
		mMinBtn8 = (Button) findViewById(R.id.min_btn8);
//		
		mMaxButtons.add(mMaxBtn1);
		mMaxButtons.add(mMaxBtn2);
		mMaxButtons.add(mMaxBtn3);
		mMaxButtons.add(mMaxBtn4);
		mMaxButtons.add(mMaxBtn5);
		mMaxButtons.add(mMaxBtn6);
		mMaxButtons.add(mMaxBtn7);
		mMaxButtons.add(mMaxBtn8);
//		
//		
		mMinButtons.add(mMinBtn1);
		mMinButtons.add(mMinBtn2);
		mMinButtons.add(mMinBtn3);
		mMinButtons.add(mMinBtn4);
		mMinButtons.add(mMinBtn5);
		mMinButtons.add(mMinBtn6);
		mMinButtons.add(mMinBtn7);
		mMinButtons.add(mMinBtn8);
//		
		for(int i = 0; i < mMaxButtons.size();i++){
			
			
			
			
			
		}
		
//--------------------------------------------------------
		
		
		for(int i = 0;i < mButtonList.size(); i++){
			mMaxButtons.get(i).setOnClickListener(mMaxButtonListener);
			mMinButtons.get(i).setOnClickListener(mMinButtonListener);
			mMyButtonforMaxButton.put(mButtonList.get(i), mMaxButtons.get(i));
			mMyButtonforMinButton.put(mButtonList.get(i),  mMinButtons.get(i));
			
			mButtonForRadioGroup.put(mButtonList.get(i), mRadioGroups.get(i));
		}


		for(int i = 0,j = 2; i < mRadioButtons.size();i++,j+=2){
			if(j < 16){
				mRadioButtonForAddr.put(mRadioButtons.get(i), "0"+(Integer.toHexString(j)));
			}else{
				mRadioButtonForAddr.put(mRadioButtons.get(i), Integer.toHexString(j));
			}
			
			if(mRadioButtons.get(i) != mCeiling1 && mRadioButtons.get(i) != mCeiling2 && mRadioButtons.get(i) != mCeiling3 && mRadioButtons.get(i) != mCeiling4 &&
					mRadioButtons.get(i) != mCeiling5 && mRadioButtons.get(i) != mCeiling6 && mRadioButtons.get(i) != mCeiling7 && mRadioButtons.get(i) != mCeiling8){
				mRadioButtons.get(i).setClickable(false);
			}
			System.out.println("mRadioButtons.get(i) = "+mRadioButtons.get(i)+"and addr = "+Integer.toHexString(j));
		}

		for(int i = 0;i < mKeys.length;i ++){
			mRadioButtonForKey.put(mRadioButtons.get(i), mKeys[i]);
			System.out.println("mRadioButtons.get(i) = "+mRadioButtons.get(i)+"mkeys[i] = "+mKeys[i]);
		}


		for(RadioGroup r:mRadioGroups){
			r.setOnCheckedChangeListener(mOnRadioButtonCheckListener);
		}


	}


	class MOnRadioButtonCheckListener implements OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			System.out.println("==============================="+mCurrentRadioButton);
			mCurrentRadioButton = (RadioButton) findViewById(checkedId);

			System.out.println("==============================="+mCurrentRadioButton);
			if(mCurrentSwitch.isChecked() != mAllPreferenceForSwitch.get(mRadioButtonForKey.get(mCurrentRadioButton))){
				setSwitchChecked();
			}else{
				setSwitchChecked();
//				isInit = false;
				setSeekBarValue(!mAllPreferenceForSwitch.get(mRadioButtonForKey.get(mCurrentRadioButton)));
			}
		}

	}


	
	class MMaxButtonListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			System.out.println("max-------------------------------------");
			setSeekBarWithValue(254);
		}
		
	}
	
	class MMinButtonListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			System.out.println("max-------------------------------------");
			setSeekBarWithValue(3);
		}
		
	}
	

	class MOnToggleButtleCheckListener implements android.widget.CompoundButton.OnCheckedChangeListener{

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
			System.out.println("mCurrentSwitch = "+mCurrentSwitch);
			System.out.println("arg0 = "+arg0);
			System.out.println("isChecked = "+isChecked);
			mAllPreferenceForSwitch.put(mRadioButtonForKey.get(mCurrentRadioButton), isChecked);

			
			setMaxMinBtnClickAble(isChecked);
			
//				if(isInit){
					proccessOnSwitchCheckChaned(isChecked);
//					isInit = false;
//				}else{
//					if(isChecked){
//						setSeekBarWithValue();
//					}else{
//						proccessOnSwitchCheckChaned(isChecked);
//					}
//				}
		}
	}



	private void proccessOnSwitchCheckChaned(boolean isChecked){
		setSeekBarValue(!isChecked);
	}
	
	
	
	

	private void SetOtherButtonUncheck(MyButton mClickButton){
		System.out.println("---------SetOtherButtonUncheck()=====================--------");
		
		if(mClickButton == mHomeBtn0){
			mClickButton.setCheck(true);
			for(MyButton b:mButtonList){
				b.setCheck(false);
			}
			return;
		}
		
		for(MyButton b:mButtonList){
			if(b != mClickButton){
				b.setCheck(false);
			}else{
				
				mClickButton.setCheck(true);
				mCurrentSeekBar = MyButtonForBarMap.get(mClickButton);
				mCurrentRadioButton = (RadioButton) findViewById(mButtonForRadioGroup.get(b).getCheckedRadioButtonId());
				SetOtherSeekBarGone(mCurrentSeekBar);
				System.out.println(MyButtonForBarMap+","+mCurrentSeekBar+","+mCurrentRadioButton);
				mCurrentPP = mButtonForPP.get(mClickButton);
				mCurrentSwitch = mButtonForSwitch.get(mClickButton);
				mCurrentImageView = mButtonForImage.get(mClickButton);
				mCurrentMaxButton = mMyButtonforMaxButton.get(mClickButton);
				mCurrentMinButton = mMyButtonforMinButton.get(mClickButton);
				setSwitchChecked();
				setSeekBarValue(!mCurrentSwitch.isChecked());


				//				proccessOnSwitchCheckChaned();
			}
		}
	}




	private void SetOtherSeekBarGone(CircularSeekBar current){
		for(CircularSeekBar b : mAllSeekBar){
			if(b == current){
				b.setVisibility(View.VISIBLE);
			}else{
				b.setVisibility(View.INVISIBLE);
			}
		}
	}
	
	
	
	

	
	class MControlButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			if(v == mAtHomeBtn){
				OpenmCeiling1(100.0f);
				return;
			}
			if(v == mOutBtn){
				TurnOffAllLight();
				return;
			}
			if(v == mSunnyBtn){
				TurnOffAllLight();
				return;
			}
			if(v == mCloudyBtn){
				TurnOffAllLight(1000,50);
				return;
			}
			if(v == mMovieBtn){
				TurnOffAllLight(1000,20);
				
				return;
			}
			if(v == mReadingBtn){
				OpenmCeiling1(100.0f);
				return;
			}
			if(v == mGameBtn){
				OpenmCeiling1(80.0f);
				return;
			}
			if(v == mSleepBtn){
				TurnOffAllLight(1000,5);
				return;
			}
			if(v == mTrunAllOnBtn){
				TurunOnAllLight();
				return;
			}
			if(v == mTrunAllOffBtn){
				TurnOffAllLight();
				return;
			}
		}
	}
	
	
	
	private void OpenmCeiling1(float percent){//打开客厅的主灯，参数是百分比，50%就是50.0
//		Toast.makeText(BluetoothConn.this, "客厅主灯打开 亮度"+percent+"%", Toast.LENGTH_LONG).show();
		int value  = (int)Math.round(((percent*MAXVALUE)/100.0f));
		mAllPreferenceForSwitch.put(key_ceiling1, true);//设置为打开，这个值后面会被读取
		mSharedPreferencesForSeekBar.edit().putBoolean(key_ceiling1, true).commit();//将值保存在文件中
		mAllPreferenceForSeekBar.put(key_ceiling1, value);//将值放到map里面
		mSharedPreferencesForSeekBar.edit().putInt(key_ceiling1, value).commit();
		send_cmd("02", value);//mHomeBtn1 是客厅按钮，根据这个按钮找到
	}
	
	
	
	private void TurnOffAllLight(final int isUseHandle,final int value){//关闭所有灯
//		Toast.makeText(BluetoothConn.this, "关闭家里的所有灯", Toast.LENGTH_LONG).show();
		for(int i = 0;i < mKeys.length;i++){
//			mAllPreferenceForSeekBar.put(mKeys[i],mSharedPreferencesForSeekBar.getInt(mKeys[i], 50));
			mAllPreferenceForSwitch.put(mKeys[i], false);
			mSharedPreferencesForSwitch.edit().putBoolean(mKeys[i], false).commit();
		}
		if(isUseHandle == 0){
			send_cmd("FE", 0);
		}else if(isUseHandle > 0){
			send_cmd("FE", 0);
			Message m = mHandler.obtainMessage(WAITONESECOND, value);
			mHandler.sendMessageDelayed(m, isUseHandle);
		}
		
	}
	
	private void TurnOffAllLight(){//关闭所有灯
//		Toast.makeText(BluetoothConn.this, "关闭家里的所有灯", Toast.LENGTH_LONG).show();
		for(int i = 0;i < mKeys.length;i++){
//			mAllPreferenceForSeekBar.put(mKeys[i],mSharedPreferencesForSeekBar.getInt(mKeys[i], 50));
			mAllPreferenceForSwitch.put(mKeys[i], false);
			mSharedPreferencesForSwitch.edit().putBoolean(mKeys[i], false).commit();
		}
			send_cmd("FE", 0);
		
	}
	
	private void TurunOnAllLight(){
//		Toast.makeText(BluetoothConn.this, "打开家里所有电灯", Toast.LENGTH_LONG).show();
		for(int i = 0;i < mKeys.length;i++){
			mAllPreferenceForSeekBar.put(mKeys[i],MAXVALUE);
			mSharedPreferencesForSeekBar.edit().putInt(mKeys[i], MAXVALUE);
			
			mAllPreferenceForSwitch.put(mKeys[i], true);
			mSharedPreferencesForSwitch.edit().putBoolean(mKeys[i], true).commit();
		}
		send_cmd("FE", MAXVALUE);
	}
	
	
	
	class MOnClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			System.out.println("onclick----------");
			//房间0被点击
			if(mHomeBtn0.isCheck()){
				i = 0;
				if(v == mHomeBtn1){
					for(int t = 0;t < 1;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn1);
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn2);
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					for(int t = 0;t <3;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn3);
						i =3;
					}
				}

				if(v == mHomeBtn4){
					for(int t = 0;t < 4;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn4);
						i = 4;
					}
				}

				if(v == mHomeBtn5){
					for(int t = 0;t < 5;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn5);
						i = 5;
					}
				}


				if(v == mHomeBtn6){
					for(int t = 0;t < 6;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn6);
						i = 6;
					}
				}


				if(v == mHomeBtn7){
					for(int t = 0;t < 7;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn7);
						i = 7;
					}
				}
				if(v == mHomeBtn8){
					for(int t = 0;t < 8;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}
			//房间1被点击
			if(mHomeBtn1.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					i = 0;
				}
				
				
				if(v == mHomeBtn2){
					for(int t = 0;t < 1;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn2);
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn3);
						i = 3;
					}
				}

				if(v == mHomeBtn4){
					for(int t = 0;t < 3;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn4);
						i = 4;
					}
				}

				if(v == mHomeBtn5){
					for(int t = 0;t < 4;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn5);
						i = 5;
					}
				}

				if(v == mHomeBtn6){
					for(int t = 0;t < 5;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn6);
						i = 6;
					}
				}


				if(v == mHomeBtn7){
					for(int t = 0;t < 6;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn7);
						i = 7;
					}
				}


				if(v == mHomeBtn8){
					for(int t = 0;t < 7;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}
			//房间2被点击
			if(mHomeBtn2.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<2;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn1);
					i = 1;
				}

				if(v == mHomeBtn3){
					flipper.showNext();
					SetOtherButtonUncheck(mHomeBtn3);
					i = 3;
				}

				if(v == mHomeBtn4){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn4);
						i = 4;
					}
				}

				if(v == mHomeBtn5){
					for(int t = 0;t < 3;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn5);
						i = 5;
					}
				}

				if(v == mHomeBtn6){
					for(int t = 0;t < 4;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn6);
						i = 6;
					}
				}


				if(v == mHomeBtn7){
					for(int t = 0;t < 5;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn7);
						i = 7;
					}
				}
				if(v == mHomeBtn8){
					for(int t = 0;t < 6;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}
			//房间3被点击
			if(mHomeBtn3.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<3;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					for(int t = 0;t<2;t++){
						SetOtherButtonUncheck(mHomeBtn1);
						flipper.showPrevious();
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn2);
					i = 2;
				}

				if(v == mHomeBtn4){
					flipper.showNext();
					SetOtherButtonUncheck(mHomeBtn4);
					i = 4;
				}

				if(v == mHomeBtn5){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn5);
						i = 5;
					}
				}

				if(v == mHomeBtn6){
					for(int t = 0;t < 3;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn6);
						i = 6;
					}
				}


				if(v == mHomeBtn7){
					for(int t = 0;t < 4;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn7);
						i = 7;
					}
				}
				if(v == mHomeBtn8){
					for(int t = 0;t < 5;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}
			//房间4被点击
			if(mHomeBtn4.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<4;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					for(int t = 0;t < 3;t++){
						SetOtherButtonUncheck(mHomeBtn1);
						flipper.showPrevious();
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					for(int t = 0;t<2;t++){
						SetOtherButtonUncheck(mHomeBtn2);
						flipper.showPrevious();
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn3);
					i = 3;
				}

				if(v == mHomeBtn5){
					flipper.showNext();
					SetOtherButtonUncheck(mHomeBtn5);
					i = 5;
				}

				if(v == mHomeBtn6){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn6);
						i = 6;
					}
				}


				if(v == mHomeBtn7){
					for(int t = 0;t < 3;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn7);
						i = 7;
					}
				}
				if(v == mHomeBtn8){
					for(int t = 0;t < 4;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}


			//房间5被点击
			if(mHomeBtn5.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<5;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					for(int t = 0;t<4;t++){
						SetOtherButtonUncheck(mHomeBtn1);
						flipper.showPrevious();
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					for(int t = 0;t<3;t++){
						SetOtherButtonUncheck(mHomeBtn2);
						flipper.showPrevious();
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					for(int t = 0;t<2;t++){
						SetOtherButtonUncheck(mHomeBtn3);
						flipper.showPrevious();
					}
					i = 3;
				}

				if(v == mHomeBtn4){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn4);
					i = 4;
				}

				if(v == mHomeBtn6){
					flipper.showNext();
					SetOtherButtonUncheck(mHomeBtn6);
					i = 6;
				}


				if(v == mHomeBtn7){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn7);
						i = 7;
					}
				}
				if(v == mHomeBtn8){
					for(int t = 0;t < 3;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}


			//房间6被点击
			if(mHomeBtn6.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<6;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					for(int t = 0;t<5;t++){
						SetOtherButtonUncheck(mHomeBtn1);
						flipper.showPrevious();
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					for(int t = 0;t<4;t++){
						SetOtherButtonUncheck(mHomeBtn2);
						flipper.showPrevious();
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					for(int t = 0;t<3;t++){
						SetOtherButtonUncheck(mHomeBtn3);
						flipper.showPrevious();
						i = 3;
					}
				}

				if(v == mHomeBtn4){
					for(int t = 0;t<2;t++){
						SetOtherButtonUncheck(mHomeBtn4);
						flipper.showPrevious();
						i = 4;
					}
				}

				if(v == mHomeBtn5){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn5);
					i = 5;
				}


				if(v == mHomeBtn7){
					flipper.showNext();
					SetOtherButtonUncheck(mHomeBtn7);
					i =7;
				}
				if(v == mHomeBtn8){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						SetOtherButtonUncheck(mHomeBtn8);
						i = 8;
					}
				}
			}

			//房间7被点击
			if(mHomeBtn7.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<7;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					for(int t = 0;t<6;t++){
						SetOtherButtonUncheck(mHomeBtn1);
						flipper.showPrevious();
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					for(int t = 0;t<5;t++){
						SetOtherButtonUncheck(mHomeBtn2);
						flipper.showPrevious();
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					for(int t = 0;t<4;t++){
						SetOtherButtonUncheck(mHomeBtn3);
						flipper.showPrevious();
						i = 3;
					}
				}

				if(v == mHomeBtn4){
					for(int t = 0;t<3;t++){
						SetOtherButtonUncheck(mHomeBtn4);
						flipper.showPrevious();
						i = 4;
					}
				}

				if(v == mHomeBtn5){
					for(int t = 0;t<2;t++){
						SetOtherButtonUncheck(mHomeBtn5);
						flipper.showPrevious();
						i = 5;
					}
				}


				if(v == mHomeBtn6){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn6);
					i = 6;
				}
				if(v == mHomeBtn8){
					flipper.showNext();
					SetOtherButtonUncheck(mHomeBtn8);
					i = 8;
				}
			}


			//房间8被点击
			if(mHomeBtn8.isCheck()){
				mHomeBtn0.setCheck(false);
				if(v == mHomeBtn0){
					for(int i = 0;i<8;i++){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn0);
					}
					i = 0;
				}
				if(v == mHomeBtn1){
					for(int t = 0;t<7;t++){
						SetOtherButtonUncheck(mHomeBtn1);
						flipper.showPrevious();
						i = 1;
					}
				}

				if(v == mHomeBtn2){
					for(int t = 0;t<6;t++){
						SetOtherButtonUncheck(mHomeBtn2);
						flipper.showPrevious();
						i = 2;
					}
				}

				if(v == mHomeBtn3){
					for(int t = 0;t<5;t++){
						SetOtherButtonUncheck(mHomeBtn3);
						flipper.showPrevious();
						i = 3;
					}
				}

				if(v == mHomeBtn4){
					for(int t = 0;t<4;t++){
						SetOtherButtonUncheck(mHomeBtn4);
						flipper.showPrevious();
						i = 4;
					}
				}

				if(v == mHomeBtn5){
					for(int t = 0;t<3;t++){
						SetOtherButtonUncheck(mHomeBtn5);
						flipper.showPrevious();
						i = 5;
					}
				}


				if(v == mHomeBtn6){
					for(int t = 0;t<2;t++){
						SetOtherButtonUncheck(mHomeBtn6);
						flipper.showPrevious();
						i = 6;
					}
				}
				if(v == mHomeBtn7){
					flipper.showPrevious();
					SetOtherButtonUncheck(mHomeBtn7);
					i = 7;
				}
			}


			MyButton b = (MyButton)v;
			if(!b.isCheck()){
				b.setCheck(true);
				SetOtherButtonUncheck(b);
				if(b == mHomeBtn1){
					flipper.showNext();
					i = 1;
				}
				if(b == mHomeBtn2){
					for(int t = 0;t < 2;t++){
						flipper.showNext();
						i = 2;
					}
				}

				if(b == mHomeBtn3){
					for(int t = 0;t < 3;t++){
						flipper.showNext();
						i = 3;
					}
				}
				if(b == mHomeBtn4){
					for(int t = 0;t < 4;t++){
						flipper.showNext();
						i = 4;
					}
				}
				if(b == mHomeBtn5){
					for(int t = 0;t < 5;t++){
						flipper.showNext();
						i = 5;
					}
				}
				if(b == mHomeBtn6){
					for(int t = 0;t < 6;t++){
						flipper.showNext();
						i = 6;
					}
				}
				if(b == mHomeBtn7){
					for(int t = 0;t < 7;t++){
						flipper.showNext();
						i = 7;
					}
				}
				if(b == mHomeBtn8){
					for(int t = 0;t < 8;t++){
						flipper.showNext();
						i = 8;
					}
				}

			}
		}

	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return this.detector.onTouchEvent(event); 
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		System.out.println("in------------>>>>>>>");

		if (e1.getX() - e2.getX() > 120) {
			if (i < 8) {
				System.out.println("111111111111111");
				i++;
				System.out.println("****************************i:=============="+i);
				switch (i) {
				case 0:
					SetOtherButtonUncheck(mHomeBtn0);
					break;
				case 1:
					mHomeBtn0.setCheck(false);
					SetOtherButtonUncheck(mHomeBtn1);
					break;
				case 2:
					SetOtherButtonUncheck(mHomeBtn2);
					break;

				case 3:
					SetOtherButtonUncheck(mHomeBtn3);
					break;
				case 4:
					SetOtherButtonUncheck(mHomeBtn4);
					break;
				case 5:
					SetOtherButtonUncheck(mHomeBtn5);
					break;
				case 6:
					SetOtherButtonUncheck(mHomeBtn6);
					break;
				case 7:
					SetOtherButtonUncheck(mHomeBtn7);
					break;

				case 8:
					SetOtherButtonUncheck(mHomeBtn8);
					break;

				default:
					break;
				}

				this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
						R.anim.animation_right_in));
				this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
						R.anim.animation_left_out));
				if(i < 9){
					this.flipper.showNext();
				}
			}
			return true;
		} 
		else if (e1.getX() - e2.getX() < -120) {
			if (i > 0) {
				i--;
				System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$  i:==============="+i);
				switch (i) {
				case 0:
					SetOtherButtonUncheck(mHomeBtn0);
					break;
				case 1:

					SetOtherButtonUncheck(mHomeBtn1);
					break;
				case 2:
					SetOtherButtonUncheck(mHomeBtn2);
					break;

				case 3:
					SetOtherButtonUncheck(mHomeBtn3);
					break;
				case 4:
					SetOtherButtonUncheck(mHomeBtn4);
					break;
				case 5:
					SetOtherButtonUncheck(mHomeBtn5);
					break;
				case 6:
					SetOtherButtonUncheck(mHomeBtn6);
					break;
				case 7:
					SetOtherButtonUncheck(mHomeBtn7);
					break;

				case 8:
					SetOtherButtonUncheck(mHomeBtn8);
					break;
				default:
					break;
				}

				System.out.println("===========================i:=============="+i);
				System.out.println("2222222222222");
				this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
						R.anim.animation_left_in));
				this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
						R.anim.animation_right_out));
				this.flipper.showPrevious();
			}
			return true;
		}
		return false;

	}



	@Override
	public void onStart() {
		super.onStart();
		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
		} else {
			setupChat();
		}
	}


	private void initSeekBar(){
		System.out.println("-----onreusme------------");
		listener = new MSeekBarChangListener();
		mAllSeekBar = new ArrayList<CircularSeekBar>();
		mButtonForPP = new HashMap<MyButton, TextView>();
		mPPList = new ArrayList<TextView>();
		mButtonForImage = new HashMap<MyButton, ImageView>();
		mImgs = new ArrayList<ImageView>();

		pp1 = (TextView) findViewById(R.id.percent1);
		pp2 = (TextView) findViewById(R.id.percent2);
		pp3 = (TextView) findViewById(R.id.percent3);
		pp4 = (TextView) findViewById(R.id.percent4);
		pp5 = (TextView) findViewById(R.id.percent5);
		pp6 = (TextView) findViewById(R.id.percent6);
		pp7 = (TextView) findViewById(R.id.percent7);
		pp8 = (TextView) findViewById(R.id.percent8);

		mImageView1 = (ImageView) findViewById(R.id.light1);
		mImageView2 = (ImageView) findViewById(R.id.light2);
		mImageView3 = (ImageView) findViewById(R.id.light3);
		mImageView4 = (ImageView) findViewById(R.id.light4);
		mImageView5 = (ImageView) findViewById(R.id.light5);
		mImageView6 = (ImageView) findViewById(R.id.light6);
		mImageView7 = (ImageView) findViewById(R.id.light7);
		mImageView8 = (ImageView) findViewById(R.id.light8);

		mPPList.add(pp1);
		mPPList.add(pp2);
		mPPList.add(pp3);
		mPPList.add(pp4);
		mPPList.add(pp5);
		mPPList.add(pp6);
		mPPList.add(pp7);
		mPPList.add(pp8);


		mImgs.add(mImageView1);
		mImgs.add(mImageView2);
		mImgs.add(mImageView3);
		mImgs.add(mImageView4);
		mImgs.add(mImageView5);
		mImgs.add(mImageView6);
		mImgs.add(mImageView7);
		mImgs.add(mImageView8);

		for(int i=0;i < mButtonList.size();i++){
			mButtonForPP.put(mButtonList.get(i), mPPList.get(i));
			mButtonForImage.put(mButtonList.get(i), mImgs.get(i));
		}


		seekbar1 = (CircularSeekBar) findViewById(R.id.seekbar1);
		seekbar1.setSeekBarChangeListener(listener);
		seekbar1.setMaxProgress(MAXVALUE);

		seekbar2 = (CircularSeekBar) findViewById(R.id.seekbar2);
		seekbar2.setSeekBarChangeListener(listener);
		seekbar2.setMaxProgress(MAXVALUE);

		seekbar3 = (CircularSeekBar) findViewById(R.id.seekbar3);
		seekbar3.setSeekBarChangeListener(listener);
		seekbar3.setMaxProgress(MAXVALUE);

		seekbar4 = (CircularSeekBar) findViewById(R.id.seekbar4);
		seekbar4.setSeekBarChangeListener(listener);
		seekbar4.setMaxProgress(MAXVALUE);

		seekbar5 = (CircularSeekBar) findViewById(R.id.seekbar5);
		seekbar5.setSeekBarChangeListener(listener);
		seekbar5.setMaxProgress(MAXVALUE);

		seekbar6 = (CircularSeekBar) findViewById(R.id.seekbar6);
		seekbar6.setSeekBarChangeListener(listener);
		seekbar6.setMaxProgress(MAXVALUE);

		seekbar7 = (CircularSeekBar) findViewById(R.id.seekbar7);
		seekbar7.setSeekBarChangeListener(listener);
		seekbar7.setMaxProgress(MAXVALUE);


		seekbar8 = (CircularSeekBar) findViewById(R.id.seekbar8);
		seekbar8.setSeekBarChangeListener(listener);
		seekbar8.setMaxProgress(254);



		mAllSeekBar.add(seekbar1);
		mAllSeekBar.add(seekbar2);
		mAllSeekBar.add(seekbar3);
		mAllSeekBar.add(seekbar4);
		mAllSeekBar.add(seekbar5);
		mAllSeekBar.add(seekbar6);
		mAllSeekBar.add(seekbar7);
		mAllSeekBar.add(seekbar8);




		MyButtonForBarMap.put(mHomeBtn1, seekbar1);
		MyButtonForBarMap.put(mHomeBtn2, seekbar2);
		MyButtonForBarMap.put(mHomeBtn3, seekbar3);
		MyButtonForBarMap.put(mHomeBtn4, seekbar4);
		MyButtonForBarMap.put(mHomeBtn5, seekbar5);
		MyButtonForBarMap.put(mHomeBtn6, seekbar6);
		MyButtonForBarMap.put(mHomeBtn7, seekbar7);
		MyButtonForBarMap.put(mHomeBtn8, seekbar8);
	}


	@Override
	public synchronized void onResume() {
		super.onResume();
		if (D)
			Log.e(TAG, "+ ON RESUME +");
		FirstReadPerference();
	}



	private void setupChat() {
		Log.d(TAG, "setupChat()");

		//		mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
		//		mConversationView = (ListView) findViewById(R.id.in);
		//		mConversationView.setAdapter(mConversationArrayAdapter);
		Intent startIntent = new Intent(BluetoothConn.this, BluetoothConnController.class);
		startService(startIntent);

		Intent i = new Intent(BluetoothConnController.GET_SERIVICE_STATUS_ACTION);
		sendBroadcast(i);
	}

	@Override
	public synchronized void onPause() {
		super.onPause();
		System.out.println("-----onPause------------");
		writePerference();
	}

	@Override
	public void onStop() {
		super.onStop();
		System.out.println("-----onStop------------");

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("destory--------------");
		unregisterReceiver(mNotiReceiver);
	}

	private void ensureDiscoverable() {
		if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
			Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
			startActivity(discoverableIntent);
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (D)
			Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
		switch (requestCode) {
		case REQUEST_CONNECT_DEVICE:
			if (resultCode == Activity.RESULT_OK) {
				String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
				Intent i = new Intent(BluetoothConnController.CONNECT_REQUEST_ACTION);
				i.putExtra(DEVICE_ADDRESS, address);
				sendBroadcast(i);
			}
			break;
		case REQUEST_DISCONNECT_DEVICE:
			if (resultCode == Activity.RESULT_OK) {
				String address = data.getExtras().getString(ConnectedDeviceListActivity.EXTRA_CONNECTED_ADDRESS);
				Intent i = new Intent(BluetoothConnController.DISCONNECT_REQUEST_ACTION);
				i.putExtra(DISCONNECT_DEVICE_ADDRESS, address);
				sendBroadcast(i);

			}
			break;
		case REQUEST_ENABLE_BT:
			if (resultCode == Activity.RESULT_OK) {
				setupChat();
			} else {
				Log.d(TAG, "BT not enabled");
				Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
				finish();
			}
			break;

		case REQUEST_PREF_SETTING:
			if (resultCode == Activity.RESULT_OK) {
				//				SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
			}
			break;

		case REQUEST_ABOUT_INFO:
			if (resultCode == Activity.RESULT_OK) {
				Log.e(TAG, "[onActivityResult] Current version is = " + versionNo);
			}
			break;
		case FILE_DIALOG_REQUEST:
			if (resultCode == Activity.RESULT_OK) {
				Log.d(TAG, "receive FILE_DIALOG_REQUEST result: " + resultCode);
				String file = data.getExtras().getString(FileDialog.RESULT_PATH);
				Intent i = new Intent(BluetoothConnController.SEND_MSG_FROM_BT_ACTION);
				i.putExtra("MESSAGE", file.toString());
				i.putExtra("MODE", BluetoothConnController.MSG_MODE_SEND_FILE);
				sendBroadcast(i);
			}

			break;

		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_ABOUT_INFO:
			return new AlertDialog.Builder(BluetoothConn.this).setIcon(R.drawable.ic_menu_help)
			.setTitle(R.string.about_title).setMessage(R.string.about_content)
			.setPositiveButton(R.string.about_dialog_ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int button) {

				}
			}).create();

		}
		return null;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onPrepareOptionsMenu");
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onMenuOpened");
		boolean status = super.onMenuOpened(featureId, menu);
		MenuItem item = (MenuItem) menu.findItem(R.id.monitor);
		item.setTitle(mMonitor ? "Stop Monitir" : "Start Monitor");
		return status;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.scan:
			// Launch the DeviceListActivity to see devices and do scan
			Intent serverIntent = new Intent(this, DeviceListActivity.class);
			startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
			break;
		case R.id.inquiry:
			// Launch the DeviceListActivity to see devices and do scan
			Intent settintIntent = new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
			startActivity(settintIntent);
			break;
		case R.id.discoverable:
			// Ensure this device is discoverable by others
			ensureDiscoverable();
			break;
		case R.id.disconnect:
			Log.i(TAG, "[onOptionsItemSelected] R.id.disconnect");
			Intent disconnectIntent = new Intent(this, ConnectedDeviceListActivity.class);
			startActivityForResult(disconnectIntent, REQUEST_DISCONNECT_DEVICE);
			break;

		case R.id.setting:
			Intent settingIntent = new Intent(this, SettingPreferencActivity.class);
			startActivityForResult(settingIntent, REQUEST_PREF_SETTING);
			break;
		case R.id.monitor:
			if (mMonitor == false) {

				mMonitor = true;
				item.setTitle("Stop Monitor");
			} else {
				item.setTitle("Start Monitor");
				mMonitor = false;
			}
			// Log.d(TAG,"monitor "+ mMonitor);
			Intent i = new Intent(BluetoothConnController.START_MONITOR_ACTION);
			i.putExtra(BluetoothConnController.MONITOR_STATUS, mMonitor);
			sendBroadcast(i);
//			mTitle = (TextView) findViewById(R.id.title_right_text);
//			mTitle.setText("monitor: " + mMonitor);
			break;
		case R.id.file_dialog:
			Intent fileDialog = new Intent(this, FileDialog.class);
			startActivityForResult(fileDialog, FILE_DIALOG_REQUEST);
			break;
		case R.id.about:
			// Intent aboutIntent = new Intent(this, AboutInfoActivity.class);
			// startActivityForResult(aboutIntent,REQUEST_PREF_SETTING );
			showDialog(DIALOG_ABOUT_INFO);
			break;
		default:
			return false;
		}
		return true;
	}

	public class NotificationReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(INCOMING_MSG)) {
				System.out.println("#zekezang##############################"+intent.getStringExtra("STR"));
			} else if (action.equals(OUTGOING_MSG)) {
			} else if (action.equals(ALERT_MSG)) {
				String displayMsg = intent.getExtras().getString("STR");
				//				mConversationArrayAdapter.add(displayMsg);
				if (intent.getExtras().getInt("COUNTER") > 0) {
					AlertDialog.Builder builder = new AlertDialog.Builder(BluetoothConn.this);
					builder.setIcon(android.R.drawable.stat_notify_error).setTitle("Error").setMessage(displayMsg)
					.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int button) {
						}
					}).create();
					AlertDialog alert = builder.create();
					alert.show();
				}
			} else if (action.equals(BluetoothConnController.GET_SERIVICE_STATUS_EVENT)) {
			} else {
				Log.e(TAG, "another action: " + action);
			}
		}

	}




	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == KeyEvent.KEYCODE_BACK){
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}


	private Intent msg_inttent = null;
	String vv = null;
	float pi = 0;
	private void send_cmd(String addr, int current_value){
		
		pi = (float)((float)current_value/254)*100;
		System.out.println("百分比    "+pi);
		if(current_value<16){
			vv = "0"+Integer.toHexString(current_value).toUpperCase();
		}else{
			vv = Integer.toHexString(current_value).toUpperCase();
		}
		
		System.out.println("send common addr = "+addr+",vv = "+vv);
		
		msg_inttent = new Intent(BluetoothConnController.SEND_MSG_FROM_BT_ACTION);
		msg_inttent.putExtra("MESSAGE", addr+" "+vv);
		msg_inttent.putExtra("MODE", BluetoothConnController.MSG_MODE_SEND_STRING);
		sendBroadcast(msg_inttent);
	}


	
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
				case UPDATETEXTVIEWANDIMAGE: 
					
				break;
				case WAITONESECOND:
					System.out.println("waited for one second");
					OpenmCeiling1(msg.arg1);
				break;
			}
		}
	};
	
	
	class MSeekBarChangListener implements CircularSeekBar.OnSeekChangeListener{

		public MSeekBarChangListener(){
			mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lightxxxx);
		}
		@Override
		public void onProgressChange(CircularSeekBar view, int newProgress) {
			// TODO Auto-generated method stub'
			System.out.println("newProgress:"+newProgress);
			int flag = 2;
			switch (flag)
			{
			case ToneLayer.FLAG_SATURATION:
				mToneLayer.setSaturation(newProgress);
				break;
			case ToneLayer.FLAG_LUM:
				mToneLayer.setLum(newProgress);
				break;
			case ToneLayer.FLAG_HUE:
				mToneLayer.setHue(newProgress > 60 ? newProgress + 60:newProgress +20);
				break;
			}
//			if((mAllPreferenceForSeekBar.get(mRadioButtonForKey.get(mCurrentRadioButton))) != newProgress ){
				mCurrentPP.setText(view.getProgressPercent()+"%");
				mCurrentImageView.setImageBitmap(mToneLayer.handleImage(mBitmap, flag));
				
				if(mCurrentSwitch.isChecked()){
					mAllPreferenceForSeekBar.put(mRadioButtonForKey.get(mCurrentRadioButton), newProgress);
					send_cmd(mRadioButtonForAddr.get(mCurrentRadioButton),newProgress);
				}else{
					send_cmd(mRadioButtonForAddr.get(mCurrentRadioButton),0);
				}
				
//			}
		}
	}
}
